#include "qbarserieswidget.h"
#include<QtWidgets/QMainWindow>
#include<QtCharts/QChartView>
#include<QtCharts/QBarSeries> // 以按类别分组的竖条表示一系列数据
#include<QtCharts/QBarSet>    // 表示条形图中一组条形图
#include<QtCharts/QLegend>    // 以折线图的形式显示数据
#include<QtCharts/QBarCategoryAxis>  // 将类别添加到图表的轴
#include<QtCharts/QValueAxis>        // 将值添加到图表的轴
#include<QtCharts/QLineSeries>
#include<QChart>
#include<QStringList>


QBarSeriesWidget::QBarSeriesWidget(QWidget *parent)
    : QMainWindow{parent}
{



}

void QBarSeriesWidget::paintEvent(QPaintEvent *event)
{
    QBarSet* pSet0 = new QBarSet("小马云");
    QBarSet* pSet1 = new QBarSet("小化腾");
    QBarSet* pSet2 = new QBarSet("小张磊");
    QBarSet* pSet3 = new QBarSet("小张朝阳");
    QBarSet* pSet4 = new QBarSet("小刘强东");

    // 条形数据
    *pSet0 << 1 << 2 << 3 << 4 << 5 << 6;
    *pSet1 << 5 << 0 << 0 << 4 << 0 << 7;
    *pSet2 << 3 << 5 << 8 << 13 << 8 << 5;
    *pSet3 << 5 << 6 << 7 << 3 << 4 << 5;
    *pSet4 << 9 << 7 << 5 << 3 << 1 << 2;

    // 添加条系列
    QBarSeries* pSeries = new QBarSeries();
    pSeries->append(pSet0);
    pSeries->append(pSet1);
    pSeries->append(pSet2);
    pSeries->append(pSet3);
    pSeries->append(pSet4);
    pSeries->setLabelsVisible(true); // 显示值

    QLineSeries* pLineSeries = new QLineSeries();
    pLineSeries->setName("趋势");
    pLineSeries->setPointsVisible(true);
    // 折线图数据
    pLineSeries->append(QPointF(0,4));
    pLineSeries->append(QPointF(1,15));
    pLineSeries->append(QPointF(2,20));
    pLineSeries->append(QPointF(3,4));
    pLineSeries->append(QPointF(4,12));
    pLineSeries->append(QPointF(5,17));

    // 创建图表
    QChart* pChart = new QChart();
    pChart->addSeries(pSeries);
    pChart->addSeries(pLineSeries);
    pChart->setTitle("线和条形图例");

    // 类别
    QStringList categroiesList;
    categroiesList << "一月"<< "二月"<< "三月"<< "四月"<< "五月"<< "六月";
    // X轴
    QBarCategoryAxis* pAxisX = new QBarCategoryAxis();
    pAxisX->append(categroiesList);
    // 将轴添加到指定的对齐视图
    pChart->addAxis(pAxisX,Qt::AlignBottom);
    pLineSeries->attachAxis(pAxisX);
    pSeries->attachAxis(pAxisX);
    pAxisX->setRange(QString("一月"),QString("六月"));

    // Y轴
    QValueAxis* pAxisY = new QValueAxis();
    pChart->addAxis(pAxisY,Qt::AlignLeft);
    pLineSeries->attachAxis(pAxisY);
    pSeries->attachAxis(pAxisY);
    pAxisY->setRange(0,20);

    // 图例
    pChart->legend()->setVisible(true);
    pChart->legend()->setAlignment(Qt::AlignBottom);

    // 视图
    QChartView* pView = new QChartView(pChart);
    pView->setRenderHint(QPainter::Antialiasing);
    setCentralWidget(pView);
}
